﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.Web;

namespace AppEszköz.Cryptography
{
    [ComVisible(false)]
    [Serializable]
    public class SecureCollection : NameValueCollection, ICollection, IEnumerable
    {
        private string cryptoKey;

        public string EncryptedString
        {
            get
            {
                return HttpUtility.UrlEncode(TripleDesEncryption.Encrypt(this.Serialize(), this.cryptoKey).Replace("<", "~#060;").Replace("=", "~#061;"));
            }
        }

        public string CryptoKey
        {
            get
            {
                return this.cryptoKey;
            }
            set
            {
                this.cryptoKey = value;
            }
        }

        public SecureCollection(string cryptoKey)
        {
            if (string.IsNullOrEmpty(cryptoKey))
                throw new ArgumentNullException("cryptoKey");
            this.cryptoKey = cryptoKey;
        }

        protected SecureCollection(SerializationInfo info, StreamingContext context)
          : base(info, context)
        {
        }

        public void LoadEncryptedValues(string encryptedText)
        {
            if (string.IsNullOrEmpty(encryptedText))
                throw new ArgumentNullException("encryptedText");
            encryptedText = encryptedText.Replace("~#060;", "<");
            encryptedText = encryptedText.Replace("~#061;", "=");
            string decryptedQueryString;
            try
            {
                decryptedQueryString = TripleDesEncryption.Decrypt(encryptedText, this.cryptoKey);
            }
            catch (CryptographicException ex)
            {
                decryptedQueryString = "";
            }
            catch (FormatException ex)
            {
                decryptedQueryString = "";
            }
            this.Deserialize(decryptedQueryString);
        }

        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

        public override string ToString()
        {
            return this.EncryptedString;
        }

        void ICollection.CopyTo(Array array, int index)
        {
            this.CopyTo(array, index);
        }

        public void CopyTo(Exception[] array, int index)
        {
            ((ICollection)this).CopyTo((Array)array, index);
        }

        private void Deserialize(string decryptedQueryString)
        {
            string str1 = decryptedQueryString;
            char[] chArray1 = new char[1]
            {
        '&'
            };
            foreach (string str2 in str1.Split(chArray1))
            {
                char[] chArray2 = new char[1]
                {
          '='
                };
                string[] strArray = str2.Split(chArray2);
                if (strArray.Length == 2)
                    this.Add(strArray[0], strArray[1]);
            }
        }

        private string Serialize()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (string index in this.AllKeys)
            {
                stringBuilder.Append(index);
                stringBuilder.Append('=');
                stringBuilder.Append(this[index]);
                stringBuilder.Append('&');
            }
            return stringBuilder.ToString();
        }
    }
}
